﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Reflection;

namespace JsonFormat
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
            label1.Text = "json字符串:";
            label2.Text = "对象信息:";
            button1.Text = "反序列化";

            Type type = this.GetType();
            TypeInfo typeInfo = type.GetTypeInfo();
            //listBox1.Items.Add(typeInfo);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if(textBox1.Text != "")
            {
                JsonClass jsonClass = new JsonClass(textBox1.Text);
                List<Order> orders = jsonClass.json2Orders();
                foreach (Order order in orders)
                {
                    foreach (PropertyInfo p in order.GetType().GetProperties())
                    {
                        if(p.PropertyType.Name == "String" || p.PropertyType.Name == "string")
                        {
                            StringBuilder sb = new StringBuilder();
                            sb.Append(p.Name).Append(":").Append(p.GetValue(order));
                            listBox1.Items.Add(sb.ToString());
                        }
                        else if (p.PropertyType.Name == "DateTime")
                        {
                            StringBuilder sb = new StringBuilder();
                            sb.Append(p.Name).Append(":").Append(p.GetValue(order));
                            listBox1.Items.Add(sb.ToString());
                        }
                        else if (p.PropertyType.Name == "Char")
                        {
                            StringBuilder sb = new StringBuilder();
                            sb.Append(p.Name).Append(":").Append(p.GetValue(order));
                            listBox1.Items.Add(sb.ToString());
                        }
                        else if (p.PropertyType.Name == "Decimal")
                        {
                            StringBuilder sb = new StringBuilder();
                            sb.Append(p.Name).Append(":").Append(p.GetValue(order));
                            listBox1.Items.Add(sb.ToString());
                        }
                        else if (p.PropertyType.Name == "Boolean")
                        {
                            StringBuilder sb = new StringBuilder();
                            sb.Append(p.Name).Append(":").Append(p.GetValue(order));
                            listBox1.Items.Add(sb.ToString());
                        }
                        else if (p.PropertyType.Name == "Goods[]")
                        {
                            Goods[] goods = order.goods;
                            foreach (Goods good in goods)
                            {
                                foreach(NV v in good.NVList())
                                {
                                    listBox1.Items.Add(v.str());
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
